package dmsxl.huisu;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Zhang Dongwei
 * Date: 2023/7/13 16:12
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 *
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 */
public class hs5_17 {

    static List<String> res = new ArrayList<>();

    public static void main(String[] args) {
        res = letterCombinations("23");
        System.out.println(res);
    }

    public static List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0){
            return res;
        }
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        combineHelper2(digits, numString, 0);
        return res;
    }

    static StringBuilder temp = new StringBuilder();

    public static void combineHelper2(String digits, String[] numString, int num){
        if (num == digits.length()){
            res.add(temp.toString());
            return;
        }
        String str = numString[digits.charAt(num) - '0'];
        for (int i = 0; i < str.length(); i++){
            temp.append(str.charAt(i));
            combineHelper2(digits, numString, num + 1);
            temp.deleteCharAt(temp.length() - 1);
        }
    }
}
